<?php

namespace App\Http\Requests;

use App\Models\StatusCode;
use App\Services\CommonReturn;

class FilesPostRequest extends CommonRequest
{
    public $file_type = 'file';//文件类型:file image
    public $file_name;//form表单上传的input name
    public $save_dir = 'file';//文件存储的路径

    /**
     * Description:  rules
     * @author: JiaMeng <666@majiameng.com>
     * Updater:
     * @return array
     */
    public function rules()
    {
        if( ($this->route('name',$this->get('name','')) == 'ueditor') && ($this->route('action',$this->get('action','')) != 'uploadimage')){
            return array();
        }

        $file = $this->file();
        if(count($file) == 1){
            $this->file_name = array_keys($file)[0];//获取form表单上传的input name
        }

        $this->save_dir = $this->route('name',$this->save_dir);
        if(in_array($this->save_dir,['ueditor','editormd'])){
            $this->save_dir = 'file';
        }
        $this->file_type = $this->get('file_type',$this->file_type);
        if(!in_array($this->file_type,['file','image'])){
            CommonReturn::fail(StatusCode::COMMON_PARAMS_VERIFY_ERROR,'文件类型错误');
        }
        return [
            $this->file_name => 'required|'.$this->file_type,
        ];
    }

    /**
     * Description:  messages
     * @author: JiaMeng <666@majiameng.com>
     * Updater:
     * @return array
     */
    public function messages()
    {
        return [
            $this->file_name.'.required'=>'请选择文件',
            $this->file_name.'.file'=>'文件格式不正确',
            $this->file_name.'.image'=>'图片格式不正确',
        ];
    }

}
